package com.baiqian.doupo.effect;

import com.baiqian.doupo.Utils;
import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.AttributeModifierManager;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectType;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;


public class EffectRegistry {
    public static final DeferredRegister<Effect> POTIONS = DeferredRegister.create(ForgeRegistries.POTIONS, Utils.MOD_ID);
    public static final RegistryObject<Effect> ju_jing_hui_shen = POTIONS.register("ju_jing_hui_shen", JuJingHuiShen::new);
    public static final RegistryObject<Effect> ling_hun_pi_bei = POTIONS.register("ling_hun_pi_bei", LingHunPiBei::new);
    public static final RegistryObject<Effect> ling_hun_ku_jie = POTIONS.register("ling_hun_ku_jie", LingHunKuJie::new);
    public static final RegistryObject<Effect> ling_hun_gan_zhi = POTIONS.register("ling_hun_gan_zhi", LingHunGanZhi::new);
    public static final RegistryObject<Effect> ju_qi_san_effect = POTIONS.register("ju_qi_san_effect", JuQiSanEffect::new);
}

//修炼效果：聚精会神
class JuJingHuiShen extends Effect{
    public JuJingHuiShen() {
        super(EffectType.HARMFUL, 5926017);
        addAttributesModifier(Attributes.MOVEMENT_SPEED, "7107DE5E-7CE8-4030-940E-514C1F160890", -0.15F, AttributeModifier.Operation.MULTIPLY_TOTAL);
    }
}

//灵魂效果：灵魂疲惫
class LingHunPiBei extends Effect{
    public LingHunPiBei() {
        super(EffectType.HARMFUL, 5926017);
        //挖掘疲劳
        addAttributesModifier(Attributes.ATTACK_SPEED, "55FCED67-E92A-486E-9800-B47F202C4386", (double)-0.1F, AttributeModifier.Operation.MULTIPLY_TOTAL);
        //缓慢
        addAttributesModifier(Attributes.MOVEMENT_SPEED, "7107DE5E-7CE8-4030-940E-514C1F160890", -0.15F, AttributeModifier.Operation.MULTIPLY_TOTAL);
        //虚弱
        addAttributesModifier(Attributes.ATTACK_DAMAGE, "22653B89-116E-49DC-9B6B-9971489B5BE5", 0.0D, AttributeModifier.Operation.ADDITION);
    }
}

//灵魂效果：灵魂枯竭
class LingHunKuJie extends Effect{
    private static int time_var =0;
    public LingHunKuJie() {
        super(EffectType.HARMFUL, 2039587);
        }

    @Override
    public void applyAttributesModifiersToEntity(LivingEntity entityLivingBaseIn, AttributeModifierManager attributeMapIn, int amplifier) {
        super.applyAttributesModifiersToEntity(entityLivingBaseIn, attributeMapIn, amplifier);
        ++time_var;
        if(time_var>=20) {
            time_var=0;
            entityLivingBaseIn.setHealth(entityLivingBaseIn.getHealth() - (1 + amplifier) * 0.02f);
        }
    }
}

//灵魂效果：灵魂感知
class LingHunGanZhi extends Effect{
    private static int time_var =0;
    public LingHunGanZhi() {
        super(EffectType.HARMFUL, 2039587);
    }

    @Override
    public void applyAttributesModifiersToEntity(LivingEntity entityLivingBaseIn, AttributeModifierManager attributeMapIn, int amplifier) {
        super.applyAttributesModifiersToEntity(entityLivingBaseIn, attributeMapIn, amplifier);
    }
}

//丹药效果：聚气散
class JuQiSanEffect extends Effect{
    protected JuQiSanEffect() {
        super(EffectType.HARMFUL, 5926017);
    }

    @Override
    public void applyAttributesModifiersToEntity(LivingEntity entityLivingBaseIn, AttributeModifierManager attributeMapIn, int amplifier) {
        super.applyAttributesModifiersToEntity(entityLivingBaseIn, attributeMapIn, amplifier);
        if(!entityLivingBaseIn.world.isRemote){
            if(entityLivingBaseIn instanceof PlayerEntity){
                LazyOptional<IDouQiCapability> douqiCap = entityLivingBaseIn.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
                if (douqiCap.isPresent()) {
                    douqiCap.ifPresent((l) -> {
                        l.addDouQi((PlayerEntity) entityLivingBaseIn);
                        if (l.getLevelBig() == 1 && l.isToLevelBig()) {
                            entityLivingBaseIn.removePotionEffect(EffectRegistry.ju_qi_san_effect.get());
                            l.setDouQiHaveMax(0.1, (PlayerEntity) entityLivingBaseIn);
                            l.setDouQi(0,2, (PlayerEntity) entityLivingBaseIn);
                            l.setLevelUpTime(30 * 20);
                        }
                    });
                }
            }
        }
    }
}